/*
Copyright 2007 Nathan Baker

This file is part of XAliens.

XAliens is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

XAliens is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with XAliens.  If not, see <http://www.gnu.org/licenses/>.
 */

package sprites;

import utils.BoundsRect;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class ShipEntity extends Entity {
    protected ArrayList<ShotEntity> _Shots = new ArrayList<ShotEntity>(1);
    private ArrayList<BufferedImage> _ShotFrames = new ArrayList<BufferedImage>();
    private int _shotVelocityX;
    private int _shotVelocityY;

    /**
     * Creates a new ShipEntity instance with an Image and a boundingBox for
     * restricting movement of the ShipEntity.
     *
     * @param img The image to load.
     * @param rct A Rectangle representing the movement area allowed.
     */
    protected ShipEntity(BufferedImage img, BoundsRect rct) {
        super(img);
        mBounds = rct;
    }

    /**
     * Creates a new ShotEntity based on mShotImg with default velocity values.
     */
    public void fireShot() {
        ShotEntity se = new ShotEntity(mBounds, null, null);
        for (BufferedImage bf : _ShotFrames) {
            se.addFrame(bf);
        }
        se.setLocation(getX() + (getWidth() / 2) - (se.getWidth() / 2), getY());
        _Shots.add(se);
    }

    /**
     * Sets a reference to an image to be used as the shot image;
     *
     * @param img The image to use for shots;
     */
    public void addShotFrame(BufferedImage img) {
        _ShotFrames.add(img);
    }

    /**
     * Sets the location of the ShipEntity and optionally checks if it will
     * exceed the boundary area.
     *
     * @param x           The x location to set.
     * @param y           The y location to set.
     * @param checkBounds Optionally check whether to check if this will exceed the
     *                    boundary area. If set to true the ShipEntity will be placed as
     *                    close to the x and y coordinates as possible.
     */
    public void setLocation(int x, int y, boolean checkBounds) {
        if (checkBounds) {
            if (x < mBounds.x)
                x = mBounds.x;
            if (y < mBounds.y)
                y = mBounds.y;
            if (x + getWidth() > mBounds.getRight())
                x = (int) (mBounds.getRight() - getWidth());
            if (y + getHeight() > mBounds.getBottom())
                y = (int) (mBounds.getBottom() - getHeight());
        }
        super.setLocation(x, y);
    }

    /**
     * Moves shots by the absolute value of dist.
     */
    @Deprecated
    public void moveShots(int dist) {
        for (int i = 0; i <= _Shots.size() - 1; i++) {
            _Shots.get(i).move(0, Math.abs(dist));
        }
    }

    /**
     * Moves all shots created by this ShipEntity.
     */
    public void moveShots() {
        for (ShotEntity se : _Shots) {
            se.move(_shotVelocityX, _shotVelocityY);
        }
    }

    /**
     * Draws all shots.
     *
     * @param g The graphics object to draw to.
     */
    public void drawShots(Graphics2D g) {
        for (int i = 0; i <= _Shots.size() - 1; i++) {
            _Shots.get(i).draw(g);
        }
    }

    /**
     * Removes any shots that are out of bounds.
     */
    public void cleanupShots() {
        for (int i = 0; i <= _Shots.size() - 1; i++) {
            if (_Shots.get(i).isOutOfBounds()) {
                _Shots.remove(i);
            }
        }
    }

    /**
     * Gets the width of the ShipEntity.
     */
    public int getWidth() {
        return super.getWidth();
    }

    /**
     * Gets the height of the ShipEntity.
     */
    public int getHeight() {
        return super.getHeight();
    }

    /**
     * Sets the VelocityX of all shots emitted by this ShipEntity
     *
     * @param vlX
     */
    public void setShotVelocityX(int vlX) {
        _shotVelocityX = vlX;
    }

    public void setShotVelocityY(int vlY) {
        _shotVelocityY = vlY;
    }

    /**
     * Attempt to draw at the current location.
     */
    public void draw(Graphics2D g) {
        try {
            super.draw(g);
        } catch (Exception e) {
            System.out.print("ShipEntity: " + e.getMessage() + "\n");
            System.out.print("ShipEntity: mImageX == "
                    + String.valueOf(mImageX) + "\n");
            System.out.print("ShipEntity: mImageY == "
                    + String.valueOf(mImageY) + "\n");
        }
    }
}
